草庐IT

c++ - C++中static的含义

全部标签

C++ vector 问题 - 'LNK2001: unresolved external symbol private: static...'

在有人指责我不查看预先存在的问题之前,我已经查看并意识到它与声明有关,但我仍然无法让它工作(可能与我使用vector有关)。Manager.h:#include"Flight.h"#ifndefmanager_h#definemanager_hclassManager{staticvectorairports;staticvectorflights;public:staticvoidloadAirports();staticvoidloadFlights();staticAirportgetAirport(stringcode);staticvectorsplit(conststrin

c++ - static 对 const 局部变量有影响吗?

想象以下声明:voidfoo(){conststd::arrayarr={/*alotofdifferentvalues*/};//dostuff}还有第二个:voidfoo(){staticconststd::arrayarr={/*alotofdifferentvalues*/};//dostuff}如果有的话,这两者之间可能存在哪些性能差异?这些解决方案是否存在任何危险? 最佳答案 暂时忘记数组。这混淆了两个不同的问题。您已经找到了解决生命周期和存储问题的答案。我将解决初始化问题。voidf(){staticconstintx

c++ - static 对 const 局部变量有影响吗?

想象以下声明:voidfoo(){conststd::arrayarr={/*alotofdifferentvalues*/};//dostuff}还有第二个:voidfoo(){staticconststd::arrayarr={/*alotofdifferentvalues*/};//dostuff}如果有的话,这两者之间可能存在哪些性能差异?这些解决方案是否存在任何危险? 最佳答案 暂时忘记数组。这混淆了两个不同的问题。您已经找到了解决生命周期和存储问题的答案。我将解决初始化问题。voidf(){staticconstintx

c++ - 在类型特征中,为什么人们使用 enum 而不是 static const 作为值?

例如,我就是这样写的,它编译和工作得很好:templatestructis_pointer{staticconstboolvalue=true;}那为什么有些人写的不那么明显templatestructis_pointer{enum{value=true};}相反?仅仅是因为staticconst变量使用了一个字节的内存,而enum没有? 最佳答案 一个显着的区别在于以下代码的编译和链接:templatestructis_pointer{};templatestructis_pointer{enum{value=true};};vo

c++ - 在类型特征中,为什么人们使用 enum 而不是 static const 作为值?

例如,我就是这样写的,它编译和工作得很好:templatestructis_pointer{staticconstboolvalue=true;}那为什么有些人写的不那么明显templatestructis_pointer{enum{value=true};}相反?仅仅是因为staticconst变量使用了一个字节的内存,而enum没有? 最佳答案 一个显着的区别在于以下代码的编译和链接:templatestructis_pointer{};templatestructis_pointer{enum{value=true};};vo

c++ - 在 C++ 中,static_cast<double>(a) 和 double(a) 有什么区别?

两者有什么区别inta;//agetssomevaluedoublepi=static_cast(a)/3;和inta;//agetssomevaluedoublepi=double(a)/3;你见过后者吗?在我看来,我在Stroustrup编写的一些片段中看到了它,但我找不到引用。 最佳答案 可能有人认为他们是在构建而不是在类型转换。考虑:some_fun(std::string("Hello"));许多人认为他们在那里调用了构造函数,而实际上他们正在执行C风格的转换。碰巧的是,强制转换会在它查看的一长串其他东西中查看目标类型的构

c++ - 在 C++ 中,static_cast<double>(a) 和 double(a) 有什么区别?

两者有什么区别inta;//agetssomevaluedoublepi=static_cast(a)/3;和inta;//agetssomevaluedoublepi=double(a)/3;你见过后者吗?在我看来,我在Stroustrup编写的一些片段中看到了它,但我找不到引用。 最佳答案 可能有人认为他们是在构建而不是在类型转换。考虑:some_fun(std::string("Hello"));许多人认为他们在那里调用了构造函数,而实际上他们正在执行C风格的转换。碰巧的是,强制转换会在它查看的一长串其他东西中查看目标类型的构

c++ - 使用static_assert时如何避免没有返回表达式的警告?

我有两个库可以使用,并且为了方便起见,我在它们使用的一些类型/结构之间编写了一个转换器。templatestructunsupportedType:std::false_type{};templateFormatBgetFormat(){static_assert(unsupportedType::value,"Thisisnotsupported!");}templateFormatBgetFormat(){returnFormatB(//someparameters);}templateFormatBgetFormat(){returnFormatB(//someotherpara

c++ - 使用static_assert时如何避免没有返回表达式的警告?

我有两个库可以使用,并且为了方便起见,我在它们使用的一些类型/结构之间编写了一个转换器。templatestructunsupportedType:std::false_type{};templateFormatBgetFormat(){static_assert(unsupportedType::value,"Thisisnotsupported!");}templateFormatBgetFormat(){returnFormatB(//someparameters);}templateFormatBgetFormat(){returnFormatB(//someotherpara

c++ - C++ 标准中短语 "constructors do not have names"的含义

在尝试理解C++标准中的“构造函数没有名称”这一短语时,我似乎在clang中发现了一个错误。有人可以证实这一点吗?VS2015andgcc拒绝此代码,我认为他们它是是正确的。至少,这是我从N4140中的§12.1[class.ctor]/2得到的印象:#includeclassA{public:A(){std::cout§12.1[class.ctor]/2在N4140中:Aconstructorisusedtoinitializeobjectsofitsclasstype.Becauseconstructorsdonothavenames,theyareneverfounddurin